//
//  ZJRWViewController.m
//  Estate
//
//  Created by kevin on 16/9/5.
//  Copyright © 2016年 ttsx. All rights reserved.
//
//
#define LINE_Y 20

#import "ZJRWViewController.h"
#import "TableTopStateCell.h"
#import "B_wuLiaoCell.h"
#import "DefaineCell.h"
#import "addJLGJViewController.h"

#import "H1_ImagesCell.h"
#import "AttachmentPhotoCell.h"
#import "UIActionSheet+Common.h"
#import "PictureViewController.h"


@interface ZJRWViewController ()

@property (strong, nonatomic) Tweet *curTweet;

@property (nonatomic, strong)UITableView  *tableView2;

@property (nonatomic, strong)UITableView  *tableView3;

@end

@implementation ZJRWViewController

- (TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        //注册单元格
        [self.tableView registerNib:[UINib nibWithNibName:@"TableTopStateCell" bundle:nil] forCellReuseIdentifier:@"topCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"tCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"fCell"];
        
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UCell"];
        
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
        
        
    }
    return _tableView;
}

- (NSMutableArray *)selectedAssetURLs{
    if(!_selectedAssetURLs){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return _selectedAssetURLs;
}


-(UITableView *)tableView2 {
    if (!_tableView2) {
        
        _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, DEF_SCREEN_WIDTH / 2 , DEF_SCREEN_HEIGHT-154) style:UITableViewStylePlain];
        self.tableView2.dataSource = self;
        self.tableView2.delegate = self;
        self.tableView2.bounces=NO;
        self.tableView2.showsVerticalScrollIndicator=NO;
        self.tableView2.separatorStyle = NO;
        self.tableView2.backgroundColor = [UIColor whiteColor];
        [_tableView2 registerClass:[AttachmentPhotoCell class] forCellReuseIdentifier:@"cell1"];
    }
    return _tableView2;
}

-(UITableView *)tableView3 {
    if (!_tableView3) {
        
        _tableView3 = [[UITableView alloc] initWithFrame:CGRectMake(DEF_SCREEN_WIDTH / 2 , 10, DEF_SCREEN_WIDTH / 2 , DEF_SCREEN_HEIGHT-154) style:UITableViewStylePlain];
        self.tableView3.dataSource = self;
        self.tableView3.delegate = self;
        self.tableView3.bounces=NO;
        self.tableView3.showsVerticalScrollIndicator=NO;
        self.tableView3.separatorStyle = NO;
        self.tableView3.backgroundColor = [UIColor whiteColor];
        [_tableView3 registerClass:[AttachmentPhotoCell class] forCellReuseIdentifier:@"cell1"];
    }
    return _tableView3;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"任务检查单";
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    _curTweet = [[Tweet alloc] init];
    
    [self.view addSubview:self.tableView];
    
    [self createView1];
    
    //添加右上方提交按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
    
}

//!!!!:保存视图
- (void)clickSave{
    //保存修改后数据 传值到上一界面
    // self.dataBlock(self.dataArr);
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)createView1{
    self.addBtn=[[UIButton alloc]initWithFrame:CGRECTS(260, 360, 100, 100)];
    [_addBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    [self.view addSubview:_addBtn];
    [_addBtn addTarget:self action:@selector(addMethold) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark--methold
- (void)addMethold{
    addJLGJViewController *addGJVC = [[addJLGJViewController alloc] init];
    addGJVC.dbVC = self;
    [self.navigationController pushViewController:addGJVC animated:YES];
}



#pragma mark- UITaleViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == self.tableView) {
        return 4;
    }else if(tableView == self.tableView2){
        
        return 1;
    }else{
    
        return 1;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.tableView) {
        if(section==0){
            return 4;
        }else if (section==1){
            return 6;
        }
        else if(section == 2){
            return 1;
        }else{
            
            return 1;
        }
        
    }else if(tableView == self.tableView2){
        
        return 1;
    }else{
    
        return 1;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            return 44;
        }else if (indexPath.section == 1){
            if (indexPath.row == 0||indexPath.row == 3||indexPath.row == 5) {
                return 44;
            }else{
                
                return 120;
            }
            
        }else if (indexPath.section == 2){
            
            return 120;
        }else{
            
            return [AttachmentPhotoCell cellHeightWithObj:_curTweet];
        }
        
    }else if(tableView == self.tableView2){
        
        
        return [AttachmentPhotoCell cellHeightWithObj:_curTweet];
    }else{
    
        return [AttachmentPhotoCell cellHeightWithObj:_curTweet];
    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (tableView == self.tableView) {
        if (section == 1) {
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 15, DEF_SCREEN_WIDTH-20, 15)];
            view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            //波浪线
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 15, DEF_SCREEN_WIDTH, 8)];
            img.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            img.image=[UIImage imageNamed:@"juchixia"];
            [view addSubview:img];
            return view;
            
        }else if(section == 2){
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 15, DEF_SCREEN_WIDTH-20, 15)];
            view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            //波浪线
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 15, DEF_SCREEN_WIDTH, 8)];
            img.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            img.image=[UIImage imageNamed:@"juchixia"];
            [view addSubview:img];
            return view;
            
        }else{
            //横线
            UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1)];
            line.backgroundColor=[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
            return line;
            
        }
        
    }else{
        //横线
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1)];
        line.backgroundColor=[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
        return line;
        
    }
}
//组尾
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (tableView == self.tableView) {
        //第一组 和倒数第二组
        if(section==0)
        {
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH-20, 15)];
            view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            //波浪线
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
            img.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            img.image=[UIImage imageNamed:@"juchishang"];
            [view addSubview:img];
            return view;
            
        }else if(section == 1){
            
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH-20, 15)];
            view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            //波浪线
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
            img.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            img.image=[UIImage imageNamed:@"juchishang"];
            [view addSubview:img];
            return view;
            
        }else if (section == 2){
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH-20, 15)];
            view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            //波浪线
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
            img.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
            img.image=[UIImage imageNamed:@"juchishang"];
            [view addSubview:img];
            return view;
            
            
            
        }else{
            //横线
            UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1)];
            line.backgroundColor=[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
            return line;
            
        }
        
    }else{
        //横线
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1)];
        line.backgroundColor=[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
        return line;
        
        
    }
    
}
//组尾高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (tableView == self.tableView) {
        return 15;
    }else{
        
        return 0.1;
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == self.tableView) {
        if (section == 0) {
            return 1;
        }else if (section == 3){
            
            return 1;
        }
        else{
            
            return 15;
        }
        
    }else{
        
        return 0.1;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                
                TableTopStateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topCell" forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.title.text=[NSString stringWithFormat:@"%@  检查任务单",[LSUtility dateNow]];
                return cell;
                
            }else if (indexPath.row == 1){
                DefaineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fCell" forIndexPath:indexPath];
                cell.title.text = @"项     目:";
                
                return cell;
                
            }else if (indexPath.row == 2){
                
                DefaineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fCell" forIndexPath:indexPath];
                cell.title.text = @"记 录 人:";
                
                return cell;
            }else{
                DefaineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fCell" forIndexPath:indexPath];
                cell.title.text = @"时     间:";
                
                return cell;
                
                
            }
        }else if (indexPath.section == 1){
            if (indexPath.row == 0) {
                DefaineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fCell" forIndexPath:indexPath];
                cell.title.text = @"检 查 类:";
                
                return cell;
                
            }else if (indexPath.row == 1){
                B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tCell" forIndexPath:indexPath];
                
                cell.title.text = @"检查内容";
                
                cell.text.editable = NO;
                cell.text.placeholder = @"";
                
                return cell;
                
            }else if (indexPath.row == 2){
                B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tCell" forIndexPath:indexPath];
                
                cell.title.text = @"检查情况";
                cell.text.editable = NO;
                cell.text.placeholder = @"";
                
                return cell;
                
                
                
            }else if (indexPath.row == 3){
                DefaineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fCell" forIndexPath:indexPath];
                cell.title.text = @"结   论:";
                
                return cell;
                
            }else if (indexPath.row == 4){
                B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tCell" forIndexPath:indexPath];
                
                cell.title.text = @"备   注";
                cell.text.editable = NO;
                cell.text.placeholder = @"";
                
                return cell;
                
            }else{
                
                DefaineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fCell" forIndexPath:indexPath];
                cell.title.text = @"分   数:";
                
                return cell;
            }
            
        }else if(indexPath.section == 2){
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tCell" forIndexPath:indexPath];
            
            cell.title.text = @"说   明";
            
            cell.text.backgroundColor = [UIColor whiteColor];
            
            cell.text.placeholder = @"";
            
            return cell;
            
        }else{
            
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UCell" forIndexPath:indexPath];
            
            [cell.contentView addSubview:self.tableView2];
            [cell.contentView addSubview:self.tableView3];
            //添加分割线
            UIView *line_1=[[UIView alloc]initWithFrame:CGRectMake(0, LINE_Y, DEF_SCREEN_WIDTH, 1)];
            
            line_1.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];
            
            UIView *line_2=[[UIView alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH/2-0.5, LINE_Y, 1, DEF_SCREEN_HEIGHT-10)];
            
            line_2.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];
            
            [cell.contentView addSubview:line_1];
            [cell.contentView addSubview:line_2];
            
            //添加文字
            UILabel *lab_1=[[UILabel alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH/8, LINE_Y/2, DEF_SCREEN_WIDTH/4, LINE_Y)];
            
            lab_1.text=@"整改前附件";
            
            lab_1.textAlignment=1;
            
            lab_1.backgroundColor=[UIColor whiteColor];
            
            lab_1.font=[UIFont systemFontOfSize:14.];
            
            [cell.contentView addSubview:lab_1];
            
            UILabel *lab_2=[[UILabel alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH/8*5, LINE_Y/2, DEF_SCREEN_WIDTH/4, LINE_Y)];
            
            lab_2.text=@"整改后附件";
            
            lab_2.textAlignment=1;
            
            lab_2.backgroundColor=[UIColor whiteColor];
            
            lab_2.font=[UIFont systemFontOfSize:14.];
            
            [cell.contentView addSubview:lab_2];
            
            return cell;
            
        }
        
    }else if(tableView == self.tableView2){
        WEAKSELF
        AttachmentPhotoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.isEdit = YES;
        cell.curTweet = _curTweet;
//        cell.addPicturesBlock = ^(){
//            [weakSelf showActionForPhoto];
//        };
        cell.deleteTweetImageBlockUrl= ^(TweetImage *toDelete,NSString *deUrl){
            //移除图片
            [weakSelf.curTweet deleteATweetImage:toDelete];
            //移除数组
            //  [weakSelf.selectedAssetURLs removeObject:deUrl];
            
            // [weakSelf refresh:nil];
        };

        return cell;
        
    }else{
        
        WEAKSELF
        AttachmentPhotoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.isEdit = YES;
        cell.curTweet = _curTweet;
        cell.addPicturesBlock = ^(){
            [weakSelf showActionForPhoto];
        };
        cell.deleteTweetImageBlockUrl= ^(TweetImage *toDelete,NSString *deUrl){
            //移除图片
            [weakSelf.curTweet deleteATweetImage:toDelete];
            //移除数组
            //  [weakSelf.selectedAssetURLs removeObject:deUrl];
            
            // [weakSelf refresh:nil];
        };
        
        return cell;
        
        
    }
    
}
#pragma mark UIActionSheet M

- (void)showActionForPhoto{
    [self.view endEditing:YES];
    //    @weakify(self);
    [[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
        
        [self photoActionSheet:sheet DismissWithButtonIndex:index];
        
    }] showInView:self.view];
}
- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //拍照
        if (_curTweet.tweetImages.count >= 9) {//照片最多这么多
            kTipAlert(@"最多只可选择9张照片，已经选满了。先去掉一张照片再拍照呗～");
            return;
        }
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = NO;//设置可编辑
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];//进入照相界面
    }else if (buttonIndex == 1){
        PictureViewController *picCtr=[[PictureViewController alloc]init];
        picCtr.flag=@"file";
        picCtr.selectedImageBlock=^(NSMutableArray *array){
            for (NSString *path in array) {
                //添加图片路径到已选数组
                 [self.selectedAssetURLs addObject:path];
            }
        };
        [self.navigationController pushViewController:picCtr animated:YES];
        
    }
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    
    //设置图片名称
    NSString *imageName=[NSString stringWithFormat:@"%@",[LSUtility getCurrentDateString]];
    //存入附件永久文件夹
    [LSUtility saveImagePermanent:image withName:imageName];
    //存入首页临时文件夹
    [LSUtility saveImage:image withName:[NSString stringWithFormat:@"%@",imageName]];
    
    //得到该照片路径
    NSString *path= [DEF_FILE_PATH stringByAppendingPathComponent:imageName];
    
    [self.curTweet addASelectedAssetURL:path];
    [self.selectedAssetURLs addObject:path];
    //刷新指定行
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}




@end
